package shiro.sso.chapter01.Filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;
import org.json.JSONObject;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;


public class RoleOrFilter extends AuthorizationFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {

        Subject subject = getSubject(request,response);
        //判断是否登陆
        if (subject.isAuthenticated()) {
            System.out.println("没有登陆");
        }

//        String[] roles = (String[])mappedValue;
//        if(roles == null || roles.length ==0){
//            return true;
//        }
//        for(String role:roles){
//            if(subject.hasRole(role)){
//                return true;
//            }
//        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        JSONObject res = new JSONObject();
        res.put("msg", "错误");
        res.put("success", "false");
        out.append(res.toString());
        return false;
    }

}